package com.atguigu.juc;

import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class Ticket{
    private int number = 50;

    /*public synchronized void sale(){
        if (number>0){
        System.out.println(Thread.currentThread().getName()+"\t"+"卖出第："+(number--) +"\t还剩下："+ number);
    }

}}*/

    Lock lock = new ReentrantLock(  );//List list = new ArrayList();
    public void sale(){
    lock.lock();
    try{
        if(number >0){
            System.out.println(Thread.currentThread().getName()+"\t"+"卖出第："+(number--) +"\t还剩下："+ number);


        }
    }finally {
        lock.unlock();
    }
        {
    }
    }
}

/**
 * @auther zzyy
 * @create 2020-03-25 12:41
 * 三个售票员    卖出      50张票
 * 如何编写企业级需要的工程化代码？
 *
 * 1 在高内聚低耦合的前提下，线程 操作  资源类,多线程开发中，一言不合先new一个资源类,线程对资源类进行什么样的操作？
 */

public class SaleTicketDemo {
    public static void main(String[] args) {
        Ticket ticket = new Ticket();
        //Thread(Runnable target, String name) Allocates a new Thread object.

       /* new Thread( new Runnable() {
            @Override
            public void run() {
                for (int i = 0;i<51;i++){
                    ticket.sale();
                }

            }
        } ,"A").start();
        new Thread( new Runnable() {
            @Override
            public void run() {
                for (int i = 0;i<51;i++){
                    ticket.sale();
                }

            }
        } ,"B").start();
        new Thread( new Runnable() {
                    @Override
                    public void run() {
                        for (int i = 0;i<51;i++){
                            ticket.sale();
                        }

                    }
                } ,"C").start();*/
       new Thread( () ->{for (int i =0;i <51;i++) ticket.sale();},"A" ).start();
        new Thread( () ->{for (int i =0;i <51;i++) ticket.sale();},"B" ).start();
        new Thread( () ->{for (int i =0;i <51;i++) ticket.sale();},"C" ).start();



    }
}
